from enum import Enum, unique

from sqlalchemy import Column, Integer, String, ForeignKey, Text, DateTime, JSON, BigInteger, TIMESTAMP, text, VARCHAR
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.orm import relationship
from datetime import datetime
import sqlalchemy as sa

from app.models.base import Base, TimestampMixin, DictData, BaseEnum
from app.utils.time_utils import now_time


#pending 待定 draft 草稿 closed 已关闭
@unique
class KnowledgeBaseStatus(BaseEnum):
    DRAFT = DictData('draft','草稿') #草稿
    FINISH = DictData('closed','已关闭') #完成
    PROCESSING = DictData('processing', '向量集合创建中') # 向量集合创建中

"""
DRAFT → (PARSING_FAILED/PROCESSING)  
PROCESSING -> CLOSED

DISABLED -> DRAFT 
    禁用的要先变为初始状态之后,再进行向量化

PARSING_FAILED -> (PARSING_FAILED/PROCESSING)  
      重新向量化
      
CLOSED -> DISABLED
    人工停用文件,删除向量数据库记录
"""
@unique
class DocumentStatus(BaseEnum):
    DRAFT = DictData('draft', '待向量化')          # 初始创建状态
    PROCESSING = DictData('processing', '向量化中') # 生成向量阶段
    PROCESSING_FAILED = DictData('processing_failed', '向量化失败')
    DISABLED = DictData('disabled', '已禁用')    # 人工停用文件,删除向量数据库记录
    CLOSED = DictData('closed', '已向量化')        # 最终完成状态

class KnowledgeBase(Base, TimestampMixin):
    __tablename__ = "knowledge_bases"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(255), nullable=False)
    description = Column(LONGTEXT)
    dimensions = Column(Integer,comment="向量", nullable=True)
    enable_sparse = Column(Integer,comment="启用稀疏[1:启用,0:停用]",default=0, nullable=False)
    enable_sparse_model = Column(VARCHAR(length=64),comment="稀疏模型",default='', nullable=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    status = Column(String(32),comment="状态[draft:草稿,closed:已关闭]", server_default="draft",default='draft')

    # Relationships
    documents = relationship("Document", back_populates="knowledge_base", cascade="all, delete-orphan")
    user = relationship("User", back_populates="knowledge_bases")


class Document(Base, TimestampMixin):
    __tablename__ = "documents"

    id = Column(Integer,comment="ID", primary_key=True, index=True)
    sub_path = Column(String(255),comment="存档目录", nullable=False)   #参考FilePathInfo
    description = Column(String(255),comment="描述", nullable=False)  #文件描述
    file_name = Column(String(255),comment="文件名", nullable=False)  # UploadFile获取到的文件名
    file_size = Column(BigInteger,comment="文件大小", nullable=False)  # File size in bytes
    content_type = Column(String(100), comment="文件类型", nullable=False)  # MIME type
    file_hash = Column(String(64), comment="SHA-256哈希值", index=True)  # SHA-256 hash of file content
    knowledge_base_id = Column(Integer, ForeignKey("knowledge_bases.id"),comment="知识库ID", nullable=False)
    status = Column(String(32),comment="状态", nullable=False, server_default="draft")
    status_time = Column(DateTime, comment="状态时间", default=now_time,server_default=text("now()"), )
    error_message = Column(Text,comment='错误信息')

    # Relationships
    knowledge_base = relationship("KnowledgeBase", back_populates="documents")

    __table_args__ = (
        # Ensure file_name is unique within each knowledge base
        sa.UniqueConstraint('knowledge_base_id', 'sub_path', name='uq_kb_file_name'),
    )




